package client;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

public class Gui extends JFrame implements AlfaHandeler {

	private JPanel mainPanel;
	private ClientMap map;
	private ServerConnection conn;
	private UnitHandeler uh;

	public Gui(ServerConnection conn) {
		super("Alfa Client");
		this.conn = conn;
		setLocation(800,400);
		mainPanel = new JPanel();
		map = new ClientMap();
		
		mainPanel.add(map);
		
		initSequence();
		
		uh = new UnitHandeler(map);
		conn.getListener().addAlfaHandeler("unit", uh);
		conn.getListener().addAlfaHandeler("event", new AlfaEventListener());
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(mainPanel);
		setVisible(true);
		pack();
	}
	
	private void initSequence() {
		conn.getListener().addAlfaHandeler("hello", this);
	}

	@Override
	public void alfaUpdate(String[] s) {
		int id=Integer.parseInt(s[1]);
		int map=Integer.parseInt(s[2]);
		int x=Integer.parseInt(s[3]);
		int y=Integer.parseInt(s[4]);
		Player p = new Player(conn, x, y);
		this.addKeyListener(p);
		uh.addUnit(id, p);
	}
}
